package com.gzw.dms.activity;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.gzw.dms.KJ.SelectDepartActivity;
import com.gzw.dms.MyApplication;
import com.gzw.dms.R;
import com.gzw.dms.base.BaseActivity;
import com.gzw.dms.bean.DriverInfo;
import com.gzw.dms.bean.JySuppliersInfo;
import com.gzw.dms.bean.OrderNumberInfo;
import com.gzw.dms.bean.UserInfo;
import com.gzw.dms.utils.HttpUtils;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.utils.T;
import com.gzw.dms.utils.UrlUtils;
import com.jauker.widget.BadgeView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class OrderNumberActivity extends BaseActivity {

    private TextView tv_ddfpnumber,tv_dpnumber,tv_qsnumber;
    private Toolbar toolbar;
    private Context mContext;
    private ImageView view_fp;
    private BadgeView badgeView,badgeView1,badgeView2;
    private List<OrderNumberInfo> lsMapInfo = new ArrayList<>();
    private LinearLayout layout_ddfp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_order_number);

    }

    @Override
    protected void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        tv_ddfpnumber = (TextView) findViewById(R.id.tv_ddfpnumber);
        tv_dpnumber = (TextView) findViewById(R.id.tv_dpnumber);
        tv_qsnumber = (TextView) findViewById(R.id.tv_qsnumber);
        view_fp = (ImageView) findViewById(R.id.view_fp);
        layout_ddfp = (LinearLayout)findViewById(R.id.layout_ddfp);

    }

    @Override
    protected void initData() {
        mContext = this;
        toolbar.setTitle("今日运单数");
        if (MyApplication.getUserInfo().getFlag().equals("1")){
            layout_ddfp.setVisibility(View.GONE);
        }
        SignShipOrder signShipOrder = new SignShipOrder();
        signShipOrder.execute();
    }

    @Override
    protected void initEvents() {
        toolbar.setNavigationOnClickListener(new MyOnClick());
        tv_ddfpnumber.setOnClickListener(new MyOnClick());
        tv_dpnumber.setOnClickListener(new MyOnClick());
        tv_qsnumber.setOnClickListener(new MyOnClick());
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 1){
            SignShipOrder signShipOrder = new SignShipOrder();
            signShipOrder.execute();
        }

    }

    class MyOnClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent mIntent = new Intent();

            switch (v.getId()) {
                case R.id.tv_ddfpnumber:
                    mIntent.putExtra("fpstste", "0");
                    mIntent.setClass(mContext, Main2Activity.class);
                    startActivityForResult(mIntent, 1);
                    break;
                case R.id.tv_dpnumber:
                    mIntent.putExtra("fpstste", "1");
                    mIntent.setClass(mContext, Main2Activity.class);
                    startActivityForResult(mIntent, 1);

                    break;

                case R.id.tv_qsnumber:
                    mIntent.putExtra("fpstste", "2");
                    mIntent.setClass(mContext, Main2Activity.class);
                    startActivityForResult(mIntent, 1);

                    break;
                default:
                    Intent mIntent1 = new Intent();
                    mIntent1.setClass(mContext, HomeActivity.class);
                    ComponentName componentName = mIntent1.resolveActivity(getPackageManager());
                    if (componentName == null) {
                        // 说明系统中不存在这个activity\
                        mIntent.setClass(mContext, HomeActivity.class);
                        startActivity(mIntent1);
                    }else{
                        Intent mIntent2 = new Intent();
                        //把返回数据存入Intent
                        mIntent2.putExtra("result", 1);
                        //设置返回数据
                        setResult(3, mIntent);
                        finish();
                    }

                    break;
            }
        }
    }

    public class SignShipOrder extends AsyncTask<DriverInfo, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {

        }

        protected String doInBackground(DriverInfo... params) {
            Map<String, Object> map = new HashMap<>();
            UserInfo userInfo = MyApplication.getUserInfo();
            map.put("userInfo", userInfo);
            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appGetShipperOrderCountInfo"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {
            if (isNull(result)) {
                T.showShort(mContext, "连接服务器异常");
            } else {

                if (result.indexOf("errorMsg") > 0) {
                    String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                    T.showShort(mContext, errorMsg);
                } else {
                    String successMsg = JsonUtil.getMsg(result, "successMsg");
                    final List<OrderNumberInfo> lsMap = JsonUtil.getPersons(successMsg, new TypeToken<List<OrderNumberInfo>>() {
                    }.getType());
                  for (int i=0; i < lsMap.size();i++){
                     OrderNumberInfo orderNumberInfo = lsMap.get(i);
                      String type = orderNumberInfo.getType();
                      String type2 = orderNumberInfo.getNumber();
                      Integer number = Integer.parseInt(type2);
                      if (type.equals("shiping_order_day")){
                          if (number != null) {
                              badgeView = new BadgeView(mContext);
                              badgeView.setTargetView(tv_ddfpnumber);
                              badgeView.setBadgeGravity(Gravity.RIGHT | Gravity.CENTER);
                              badgeView.setBadgeCount(number);
                              badgeView.setTextSize(18);
                          }
                      }else if (type.equals("drivertoorder_day")){
                          if (number != null) {
                              badgeView1 = new BadgeView(mContext);
                              badgeView1.setTargetView(tv_dpnumber);
                              badgeView1.setBadgeGravity(Gravity.RIGHT | Gravity.CENTER);
                              badgeView1.setBadgeCount(number);
                              badgeView1.setTextSize(18);
                          }
                      }else if (type.equals("sign_order_day")){
                          if (number != null) {
                              badgeView2 = new BadgeView(mContext);
                              badgeView2.setTargetView(tv_qsnumber);
                              badgeView2.setBadgeGravity(Gravity.RIGHT | Gravity.CENTER);
                              badgeView2.setBadgeCount(number);
                              badgeView2.setTextSize(18);
                          }
                    }
                  }


                }
            }
        }

        @Override
        protected void onCancelled() {

        }
    }

}
